package com.example.a4_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
        CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup radGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        radGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radiobtn = (RadioButton) findViewById(i);
                Toast.makeText(getApplicationContext(), "按钮组值发生改变， 你选了"
                        + radiobtn.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        Button btn1 = (Button) findViewById(R.id.button1);
        final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                    if (radioButton.isChecked()) {
                        Toast.makeText(getApplicationContext(), "点击提交按钮，获取你选择的是："
                                + radioButton.getText(), Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }
        });

        CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        Button btn = (Button) findViewById(R.id.button2);
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(this,"ok", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (compoundButton.getId() == R.id.checkBox1) {
            if (b) {
                Toast.makeText(this, "You choice checkbox1", Toast.LENGTH_LONG).show();
            }
        }
        if (compoundButton.getId() == R.id.checkBox2) {
            if (b) {
                Toast.makeText(this, "You choice checkbox2", Toast.LENGTH_LONG).show();
            }
        }
        if (compoundButton.getId() == R.id.checkBox3) {
            if (b) {
                Toast.makeText(this, "You choice checkbox3", Toast.LENGTH_LONG).show();
            }
        }
    }
}